/**
 * 静态分析总览
 *
 * Created by nongzhizhong on 2022/08/11.
 */
const chai = require('chai');
chai.use(require('chai-http'));
const common = require('../../common');
const Administrator = require('../../../models/administrator');
const uri = require('../uri');
const benchmark = require('./../../../benchmark/static-analyze/gjb.json');

module.exports = describe('/GET /project-versions/:projectVersionId/static-analyze/overview.json', function () {
  const administrator = new Administrator();
  let newUser;
  let project;
  let projectVersion;

  function url(versionId) {
    if (!versionId) {
      versionId = projectVersion.versionId;
    }
    return `/project-versions/${versionId}/static-analyze/overview.json`;
  }

  before(async function () {
    await administrator.login();
    newUser = await administrator.createRandomTester();
    project = await newUser.createRandomProject('c++');
    projectVersion = await project.createStaticAnalyzeVersion(
      [],
      benchmark.files,
      newUser
    );
    await projectVersion.rootDirectory.staticAnalyze();
    await projectVersion.project.creator.waiting4TasksOfVersionFinished(
      projectVersion.versionId
    );
  });
  after(async function () {
    await administrator.removeUsers([newUser.userId]);
  });

  common.should.be.user(uri, url, 'get');
  common.should.be.tester(uri, url, 'get');

  it('get static analyze overview of project version', function (done) {
    chai.request(uri)
      .get(url())
      .set('Authorization', `Bearer ${newUser.token.accessToken}`)
      .end((err, res) => {
        res.should.have.status(200);
        const { overview } = benchmark;
        const overviewRet = res.body;
        overview.levels.forEach((level) => {
          const levelRet = overviewRet.levels.find((lvl) => lvl.level === level.level);
          chai.assert.equal(levelRet.total, level.total);
          chai.assert.equal(levelRet.kloc, level.kloc);
        });
        overview.files.forEach((file) => {
          const fileRet = overviewRet.files.find((f) => f.filePath === file.filePath);
          chai.assert.equal(file.total, fileRet.total);
        });
        overview.rules.forEach((rule) => {
          const fileRet = overviewRet.rules.find((r) => r.ruleCode === rule.ruleCode);
          chai.assert.equal(rule.total, fileRet.total);
        });
        done();
      });
  });
});
